﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Lucene.Linq.Mapping;
using Lucene.Linq.Search;

namespace Lucene.Linq.VSTests.DatabaseIntegration.LinqToSql
{
    [Document]
    [Table(Name = "dbo.Customers")]
    public partial class Customer : IIndexable, INotifyPropertyChanging, INotifyPropertyChanged
    {
        #region Fields

        private static readonly PropertyChangingEventArgs emptyChangingEventArgs =
            new PropertyChangingEventArgs(String.Empty);

        private string _Address;
        private string _City;

        private string _CompanyName;
        private string _ContactName;
        private string _ContactTitle;
        private string _Country;
        private string _CustomerID;
        private string _Fax;
        private EntitySet<Order> _Orders;
        private string _Phone;
        private string _PostalCode;
        private string _Region;

        #endregion

        #region Extensibility Method Definitions

        partial void OnLoaded();
        partial void OnValidate();
        partial void OnCreated();
        partial void OnCustomerIDChanging(string value);
        partial void OnCustomerIDChanged();
        partial void OnCompanyNameChanging(string value);
        partial void OnCompanyNameChanged();
        partial void OnContactNameChanging(string value);
        partial void OnContactNameChanged();
        partial void OnContactTitleChanging(string value);
        partial void OnContactTitleChanged();
        partial void OnAddressChanging(string value);
        partial void OnAddressChanged();
        partial void OnCityChanging(string value);
        partial void OnCityChanged();
        partial void OnRegionChanging(string value);
        partial void OnRegionChanged();
        partial void OnPostalCodeChanging(string value);
        partial void OnPostalCodeChanged();
        partial void OnCountryChanging(string value);
        partial void OnCountryChanged();
        partial void OnPhoneChanging(string value);
        partial void OnPhoneChanged();
        partial void OnFaxChanging(string value);
        partial void OnFaxChanged();

        #endregion

        #region Constructors

        public Customer()
        {
            OnCreated();
            //this._Orders = new EntitySet<Order>(new Action<Order>(this.attach_Orders), new Action<Order>(this.detach_Orders));
        }

        #endregion

        #region Properties

        public DateTime StartDate { get; set; }

        [Field(FieldIndex.Tokenized, FieldStore.Yes)]
        [Column(Storage = "_CustomerID", DbType = "NChar(5) NOT NULL", CanBeNull = false, IsPrimaryKey = true)]
        public string CustomerID
        {
            get { return _CustomerID; }
            set
            {
                if ((_CustomerID != value))
                {
                    OnCustomerIDChanging(value);
                    SendPropertyChanging();
                    _CustomerID = value;
                    SendPropertyChanged("CustomerID");
                    OnCustomerIDChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes, IsDefault = true)]
        [Column(Storage = "_CompanyName", DbType = "NVarChar(40) NOT NULL", CanBeNull = false)]
        public string CompanyName
        {
            get { return _CompanyName; }
            set
            {
                if ((_CompanyName != value))
                {
                    OnCompanyNameChanging(value);
                    SendPropertyChanging();
                    _CompanyName = value;
                    SendPropertyChanged("CompanyName");
                    OnCompanyNameChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes, Name = "ContactName")]
        [Column(Storage = "_ContactName", DbType = "NVarChar(30)")]
        public string ContactName
        {
            get { return _ContactName; }
            set
            {
                if ((_ContactName != value))
                {
                    OnContactNameChanging(value);
                    SendPropertyChanging();
                    _ContactName = value;
                    SendPropertyChanged("ContactName");
                    OnContactNameChanged();
                }
            }
        }

        [Field(FieldIndex.Tokenized, FieldStore.Yes)]
        [Column(Storage = "_ContactTitle", DbType = "NVarChar(30)")]
        public string ContactTitle
        {
            get { return _ContactTitle; }
            set
            {
                if ((_ContactTitle != value))
                {
                    OnContactTitleChanging(value);
                    SendPropertyChanging();
                    _ContactTitle = value;
                    SendPropertyChanged("ContactTitle");
                    OnContactTitleChanged();
                }
            }
        }

        [Column(Storage = "_Address", DbType = "NVarChar(60)")]
        public string Address
        {
            get { return _Address; }
            set
            {
                if ((_Address != value))
                {
                    OnAddressChanging(value);
                    SendPropertyChanging();
                    _Address = value;
                    SendPropertyChanged("Address");
                    OnAddressChanged();
                }
            }
        }

        [Column(Storage = "_City", DbType = "NVarChar(15)")]
        public string City
        {
            get { return _City; }
            set
            {
                if ((_City != value))
                {
                    OnCityChanging(value);
                    SendPropertyChanging();
                    _City = value;
                    SendPropertyChanged("City");
                    OnCityChanged();
                }
            }
        }

        [Column(Storage = "_Region", DbType = "NVarChar(15)")]
        public string Region
        {
            get { return _Region; }
            set
            {
                if ((_Region != value))
                {
                    OnRegionChanging(value);
                    SendPropertyChanging();
                    _Region = value;
                    SendPropertyChanged("Region");
                    OnRegionChanged();
                }
            }
        }

        [Column(Storage = "_PostalCode", DbType = "NVarChar(10)")]
        public string PostalCode
        {
            get { return _PostalCode; }
            set
            {
                if ((_PostalCode != value))
                {
                    OnPostalCodeChanging(value);
                    SendPropertyChanging();
                    _PostalCode = value;
                    SendPropertyChanged("PostalCode");
                    OnPostalCodeChanged();
                }
            }
        }

        [Column(Storage = "_Country", DbType = "NVarChar(15)")]
        public string Country
        {
            get { return _Country; }
            set
            {
                if ((_Country != value))
                {
                    OnCountryChanging(value);
                    SendPropertyChanging();
                    _Country = value;
                    SendPropertyChanged("Country");
                    OnCountryChanged();
                }
            }
        }

        [Column(Storage = "_Phone", DbType = "NVarChar(24)")]
        public string Phone
        {
            get { return _Phone; }
            set
            {
                if ((_Phone != value))
                {
                    OnPhoneChanging(value);
                    SendPropertyChanging();
                    _Phone = value;
                    SendPropertyChanged("Phone");
                    OnPhoneChanged();
                }
            }
        }

        [Column(Storage = "_Fax", DbType = "NVarChar(24)")]
        public string Fax
        {
            get { return _Fax; }
            set
            {
                if ((_Fax != value))
                {
                    OnFaxChanging(value);
                    SendPropertyChanging();
                    _Fax = value;
                    SendPropertyChanged("Fax");
                    OnFaxChanged();
                }
            }
        }

        //[Association(Name = "Customer_Order", Storage = "_Orders", OtherKey = "CustomerID")]
        //public EntitySet<Order> Orders
        //{
        //  get
        //  {
        //    return this._Orders;
        //  }
        //  set
        //  {
        //    this._Orders.Assign(value);
        //  }
        //}

        #endregion

        #region Events

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        #endregion

        #endregion

        #region Methods

        protected virtual void SendPropertyChanging()
        {
            if ((PropertyChanging != null))
            {
                PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((PropertyChanged != null))
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        //private void attach_Orders(Order entity)
        //{
        //  this.SendPropertyChanging();
        //  entity.Customer = this;
        //  this.SendPropertyChanged("Orders");
        //}

        //private void detach_Orders(Order entity)
        //{
        //  this.SendPropertyChanging();
        //  entity.Customer = null;
        //  this.SendPropertyChanged("Orders");
        //}

        #endregion
    }
}
